﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="GetTaskCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace HpcSchedulerManagement.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using RestClient;

    public class GetTaskCommand : HpcSchedulerCommandBase
    {
        private readonly string hpcClusterName;

        private readonly int jobId;

        private readonly int taskId;

        private readonly IEnumerable<string> properties;

        private readonly ResponseRenderType responseRenderType;

        public GetTaskCommand(
                    IHpcSchedulerServiceContext serviceContext, 
                    string hpcClusterName, 
                    int jobId, 
                    int taskId, 
                    IEnumerable<string> properties,
                    ResponseRenderType responseRenderType = ResponseRenderType.RestPropRender)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(hpcClusterName))
            {
                throw new ArgumentException("Hpc cluster name must not be null or empty.", "hpcClusterName");
            }

            if (jobId <= 0)
            {
                throw new ArgumentException("Job id must not be less than or equal to 0.", "jobId");
            }

            if (taskId <= 0)
            {
                throw new ArgumentException("Task id must not be less than or equal to 0.", "taskId");
            }

            this.hpcClusterName = hpcClusterName;
            this.jobId = jobId;
            this.taskId = taskId;
            this.properties = properties;
            this.responseRenderType = responseRenderType;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreateGetOperationRequestContext();
        }

        protected override string FormatPath(IHpcSchedulerServiceContext serviceContext)
        {
            return string.Format("{0}/Job/{1}/Task/{2}", this.hpcClusterName, this.jobId, this.taskId);
        }

        protected override void AddParameters(RequestContext requestContext)
        {
            base.AddParameters(requestContext);

            if (this.properties != null && this.properties.Any())
            {
                requestContext.AddParameter(HpcSchedulerServiceQueryParameters.Properties, string.Join(",", this.properties));
            }

            requestContext.AddParameter(HpcSchedulerServiceQueryParameters.Render, this.responseRenderType.ToString());
        }
    }
}